package dm1101.demo04;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/1 16:45
 */
public class BankCard {
    private double balance;
    //表示有钱没钱: true表示有钱  false表示没钱
    private boolean flag=false;
    public synchronized void save(double money){
        System.out.println(Thread.currentThread().getName());
        if (flag==true){
            try {
                //等待---释放锁资源
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        balance=balance+money;
        System.out.println(Thread.currentThread().getName()+"存入"+money+"元,卡中余额："+balance);
        flag=true;
        //唤醒等待的线程
        notify();
    }

    public synchronized void token(double money){
        System.out.println(Thread.currentThread().getName());
        if (flag==false){
            try {
                //等待---释放锁资源
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        balance=balance-money;

        System.out.println(Thread.currentThread().getName()+"取出"+money+"元,卡中余额："+balance);
        flag=false;
        //唤醒等待的线程
        notify();
    }



}
